<?

require_once('DataObject/DomainModel.php');
require_once('Statuses.php');
require_once('StatusesCatalogues.php');
require_once('Files.php');
require_once('FilesTypes.php');

class ShippingServices extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=smallint, maxLength=, allowNulls=no
	protected $service;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=no
	protected $description;	//primaryKey=no, dataType=varchar, maxLength=250, allowNulls=yes
	protected $mainImageId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=yes
	protected $price;	//primaryKey=no, dataType=double, maxLength=, allowNulls=no
	
	protected $mainImage;
	protected $config;
	
	public function getConfig(){
		if (is_null($this->config)){
			$registry = Zend_Registry::getInstance();
			$config = $registry->get('config');
			$this->config = $config->uploads->shippingServices;
		}
		
		return $this->config;
	}
	
	public function getMainImage(){
		if (is_null($this->mainImage)){
			$this->mainImage = ModelMapper::instance('Files')->find($this->mainImageId);
		}
		
		return $this->mainImage;
	}
	
	public function getImageUrl(){
		if (!is_null($this->mainImageId)){
			return $this->getConfig()->origin_dir.'/'.$this->getMainImage()->getId().'.'.$this->getMainImage()->getExtension();
		}
		
	}
	
	function getThumb($width = '', $height = ''){
		if ($width == ''){
			$width = $this->getConfig()->thumbWidth;
		}
			
		if ($height == ''){
			$height = $this->getConfig()->thumbHeight;
		}
		
		if ($this->mainImageId != null){
			$image = '<img src="'.$this->getMainImage()->createThumb($this->getConfig()->origin_dir, $width, $height).'" border="0" class="'.$this->getConfig()->cssId.'" />';
		} else {
			$tExt = 'png';
			$tClass = $this->getConfig()->cssId;
			$image = DataUtils::genThumb($width, $height, $this->getConfig()->no_file_image, $tClass, $tExt);
		}
		return $image;
	}
}

class ShippingServicesMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'shipping_services';
		$this->selectViewName = 'v_shipping_services';
		$this->selectFields = '
				id,
				service,
				main_image_id,
				description,
				price,
				f_id,
				f_file_types_id,
				f_object_id,
				f_name,
				f_size,
				f_extension,
				f_type,
				f_users_id,
				f_add_date,
				f_description,
				ft_id,
				ft_type
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_shipping_services (@p_id,
				?,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_shipping_services (
				?,
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_shipping_services (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setService($array['service']);
		$obj->setDescription($array['description']);
		$obj->setMainImageId($array['main_image_id']);
		$obj->setPrice($array['price']);

		$arr = ArrayUtils::splitByPrefix($array, array('f_','ft_'));
		if (!empty($arr['f_']))
			ModelMapper::instance('Files')->loadArray($arr['f_']);
		if (!empty($arr['ft_']))
			ModelMapper::instance('FilesTypes')->loadArray($arr['ft_']);
		
		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getService(),
			$obj->getDescription(),
			$obj->getMainImageId(),
			$obj->getPrice()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getService(),
			$obj->getDescription(),
			$obj->getMainImageId(),
			$obj->getPrice()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE shipping_services');
		return $row['Auto_increment'];

	}
}

class ShippingServicesCollection extends ModelCollection {
	function add(ShippingServices $model){
		$this->doAdd($model);
	}
}

class ShippingServicesCollectionExtended extends ShippingServicesCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>